<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
        思路1：暴力法，找出所有的子数组，最后返回长度最小的
        */
      var minSubArrayLen = function (target, nums) {
        let res = [];
        for (let i = 0; i < nums.length; i++) {
          for (let j = i; j < nums.length; j++) {
            let temp = nums.slice(i, j + 1);
            let sum = temp.reduce((pre, item) => (pre += item));
            if (sum >= target) {
              res.push(temp);
            }
          }
        }
        let min = Infinity;
      
        res.forEach((arr) => {
          min = Math.min(min, arr.length);
        });
        return min == Infinity ? 0 : min;
      };
      console.log(minSubArrayLen(11, [1, 2, 3, 4, 5]));
    </script>
  </body>
</html>
